package com.chainyoung.component_tmperson.ui.activity.hdwallet;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chainyoung.common.adpter.BaseFragmentAdapter;
import com.chainyoung.common.base.BaseActivity;
import com.chainyoung.common.di.AppComponent;
import com.chainyoung.common.utils.ToastUtil;
import com.chainyoung.component_tmperson.R;
import com.chainyoung.component_tmperson.R2;
import com.chainyoung.component_tmperson.ui.activity.ScanQrCodeActivity;
import com.chainyoung.component_tmperson.ui.fragment.ImportWalletByMnemonicFragment;
import com.chainyoung.component_tmperson.ui.fragment.ImportWalletByPrivateKeyFragment;
import com.chainyoung.component_tmperson.ui.fragment.MyPurseAddrListFragment;
import com.flyco.tablayout.SlidingTabLayout;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.chainyoung.common.appcommon.hdwallet.CoinConstant.ETH;


/**
 * 导入钱包
 */
public class ImportHdWalletActivity extends BaseActivity {

    @BindView(R2.id.tab_layout)
    SlidingTabLayout tabLayout;
    @BindView(R2.id.view_pager)
    ViewPager viewPager;


    private List<Fragment> fragmentList = new ArrayList<>();


    private List<String> titles = new ArrayList<>();

    private String coinType;

    private final static int REQUEST_WORDS = 0x1111;

    private final static int REQUEST_PRIVATE_KEY = 0x1112;


    @Override
    protected void setupActivityComponent(AppComponent appComponent) {

    }

    @Override
    protected void initUI() {
        setCurrentTitle("导入钱包");
        showAndSetBarRightIcon(R.mipmap.ic_scan);
        setTabLayout();
        coinType = getIntent().getStringExtra("coinType");
    }

    private void setTabLayout() {
        fragmentList.add(ImportWalletByMnemonicFragment.newInstance(TextUtils.isEmpty(coinType) ? ETH : coinType));//暂时默认ETH
        fragmentList.add(ImportWalletByPrivateKeyFragment.newInstance(TextUtils.isEmpty(coinType) ? ETH : coinType));//暂时默认ETH
        titles.add("助记词导入");
        titles.add("私钥导入");
        //tabLayout.setTabMode(TabLayout.MODE_FIXED);
        viewPager.setAdapter(new BaseFragmentAdapter(getSupportFragmentManager(), fragmentList, titles));
        tabLayout.setViewPager(viewPager);
        viewPager.setOffscreenPageLimit(titles.size());
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.tmperson_import_hd_wallet;
    }


    @Override
    public void onRightImgClicked(View v) {
        super.onRightImgClicked(v);
        if (viewPager.getCurrentItem() == 0) {
            launchForResult(ScanQrCodeActivity.class, REQUEST_WORDS);//助记词导入
        } else {
            launchForResult(ScanQrCodeActivity.class, REQUEST_PRIVATE_KEY); //私钥导入
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }
        String result = data.getStringExtra("data");
        switch (requestCode) {
            case REQUEST_WORDS:
                checkWords(result);
                break;
            case REQUEST_PRIVATE_KEY:
                checkPrivateKey(result);
                break;
        }


    }

    /**
     * 扫描助记词导入钱包
     *
     * @param scanResult
     */
    private void checkWords(String scanResult) {
        List<String> wordsList = new ArrayList<>();
        if (scanResult.contains(":")) {
            scanResult = scanResult.substring(scanResult.lastIndexOf(":") + 1);
        }
        wordsList.clear();
        try {
            wordsList.addAll(Arrays.asList(scanResult.split(" ")));
        } catch (Exception ignore) {

        }
        if (wordsList.size() == 12) {
            ((ImportWalletByMnemonicFragment) fragmentList.get(0)).restoreView(wordsList);
        } else {
            ToastUtil.showToast("请扫描正确的助记词二维码");
            return;
        }
    }

    /**
     * 扫描privatekey 导入钱包
     *
     * @param scanResult
     */
    private void checkPrivateKey(String scanResult) {
        ((ImportWalletByPrivateKeyFragment) fragmentList.get(1)).setPrivateKey(scanResult);
    }


}
